package com.singbon.server.config;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/**
 * The type Custom logout success handler.
 */
@Slf4j
@Component
public class CustomLogoutSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler implements LogoutSuccessHandler {
  //  @Resource
  //  private OAuth2RestTemplate restTemplate;
  @Override
  protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
    System.out.println("authentication.0.0.= " + authentication);
    super.handle(request, response, authentication);
    System.out.println("authentication.0.1.= " + authentication);
  }
  @Override
  public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
                              //
                              Authentication authentication) throws IOException, ServletException {

    // region 将子系统的cookie删掉
    System.out.println("删除 cookie ");
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
      for (Cookie cookie : cookies) {
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
      }
    }
    String requestHeaderReferer = request.getHeader("referer");
    System.out.println("requestHeaderReferer = " + requestHeaderReferer);
    // 这句非常重要 requestHeaderReferer = http://localhost:8082/
    response.sendRedirect(requestHeaderReferer);
    // endregion 将子系统的cookie删掉
    // region 再次遍历cookie
    System.out.println("再次遍历cookie session start");
    System.out.println("再次遍历cookie session over");
    //    OAuth2AccessToken oAuth2AccessToken = this.restTemplate.getAccessToken();
    //    String token = oAuth2AccessToken.getValue();
    //    System.out.println("token = " + token);
    // endregion 再次遍历cookie
    if (null == authentication) {
      System.out.println("authentication1.1 = null");
    }
    else {
      System.out.println("authentication1.2 = " + cn.hutool.json.JSONUtil.toJsonStr(authentication));
      try {
        System.out.println("authentication1.3 = " + JSON.toJSONString(authentication));
      }
      catch (Exception e) {
        log.error(e.getMessage());
      }
    }
    handle(request, response, authentication);
    makeSureSth(request, response);
    System.out.println("=====================================================");
  }
  /**
   * 一些需要确定的 内容
   *
   * @param request  HttpServletRequest
   * @param response HttpServletResponse
   */
  public void makeSureSth(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("需要再三确定的内容 start");
    // 准备替换 url
    String replacedUrl = request.getRequestURL().toString().replace(request.getRequestURI(), "");
    System.out.println("replace = " + replacedUrl);
    System.out.println("|==========================|");
    String authHeader = request.getHeader("Authorization");
    System.out.println("authHeader = " + authHeader);
    System.out.println("|==========================|");
    System.out.println("response = " + (cn.hutool.json.JSONUtil.toJsonStr(response)));
    System.out.println("request = " + cn.hutool.json.JSONUtil.toJsonStr(request));
    System.out.println("|==========================|");
    try {
      System.out.println("response = " + (JSON.toJSONString(response)));
    }
    catch (Exception e) {
      log.error(e.getMessage());
    }
    try {
      System.out.println("request = " + (JSON.toJSONString(request)));
    }
    catch (Exception e) {
      log.error(e.getMessage());
    }
    System.out.println("|==========================|");
    System.out.println("需要再三确定的内容 over");
  }
}
